import Poster from '../../ccmini-cmpts/lib/wxa-plugin-canvas/poster/poster.js'

const ccminiPageHelper = require('../../helper/ccmini_page_helper.js');
const ccminiHelper = require('../../helper/ccmini_helper.js');
var base = require('../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数


Page({

    /**
     * 页面的初始数据
     */
    data: {
        // windowWidth: wx.getSystemInfoSync().windowWidth,
        picID: 1,
        // 当前页面
        facePic: 'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face01.png',
        userPic: '/cel/images/face/white.png',
        day: 1,
        proIdx: 'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face01.png',
        current: 0, //当前图片
        proList: [
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face01.png',
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face02.png',
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face03.png',
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face04.png',
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face05.png',
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face06.png',
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face07.png',
            'https://6a6f-joker-9gy49v1231338687-1308039160.tcb.qcloud.la/face1/face08.png'
        ],
        showView: true,
        language:"中文"
    },

    getDaysBetween: function (dateString1, dateString2) {
        var startDate = Date.parse(dateString1);
        var endDate = Date.parse(dateString2);
        if (startDate > endDate) {
            return 0;
        }
        if (startDate === endDate) {
            return 0;
        }
        return parseInt((endDate - startDate) / (24 * 60 * 60 * 1000))
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {

        const appBaseInfo = wx.getAppBaseInfo()

        if (appBaseInfo.language === 'zh_CN') {
            wx.setStorageSync('Language', 'zh_CN')
            this.setData({
                language: 'English',
            })
        } else {
            wx.setStorageSync('Language', 'en')
            this.setData({
                language: '中文',
            })
        };
        this.setData({
            _t: base._t(), //翻译
        });
    }
    ,

// 显示图像
    bindPreviewTap: function () {
        wx.previewImage({
            current: [this.data.facePic], // 当前显示图片的http链接
            urls: [this.data.facePic] // 需要预览的图片http链接列表
        })
    },

    switchLanguage: function() {
        if (base.getLanguage() === 'zh_CN'){
            // console.log('切换至英文');
            wx.setStorageSync('Language', 'en'); // 利用本地缓存存放用户中英文选项
            this.setData({
                language: '中文',
            })
        }else{
            // console.log('切换至中文');
            this.setData({
                language: 'English',
            })
            wx.setStorageSync('Language', 'zh_CN');
        };
        this.setData({
            _t: base._t(), //翻译
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        // let i = this.getDaysBetween(new Date().toDateString(), "2021-12-18")
        // this.setData({
        //     day: i
        // })
        // if (i < 0) {
        //     this.setData({
        //         showView: false
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: async function () {
        await this.catchCreatePosterTap();
    }
    ,

    bindPosterSuccessListener(e) {
        let facePic = e.detail;
        this.setData({
            facePic
        });
    }
    ,
    bindPosterFailListener(e) {

    },
    /**
     * 绘制放心框
     * @param e
     * @returns {Promise<void>}
     */
    bindSelectedTap: async function (e) {
        let proIdx = e.currentTarget.dataset.idx;
        this.setData({
            proIdx
        });
        await this.catchCreatePosterTap();
    }
    ,
    /**
     * 异步生成海报
     */
    catchCreatePosterTap: async function () {
        let posterConfig = {};
        posterConfig = this.makeFace();
        this.setData({
            posterConfig
        }, async () => {
            wx.showToast({
                title: '生成中',
                icon: 'loading',
                duration: 10000
            })
            await Poster.create(true);
            wx.hideToast();
        });
    }
    ,

// 绘制方形框的配置
    makeFace: function () {

        let posterConfig = {
            width: 500,
            height: 500,
            pixelRatio: 3,
            backgroundColor: '#eee',
            debug: false,
        }
        let images = [];
        images = [{
            x: 0,
            y: 0,
            url: this.data.proIdx,
            width: 500,
            height: 500,
            zIndex: 9999
        }];
        if (this.data.userPic) {
            let mainImg = {
                x: 0,
                y: 0,
                url: this.data.userPic,
                width: 500,
                height: 500,
            }
            images.push(mainImg);
        }
        posterConfig.images = images;
        return posterConfig;
    }
    ,

    onPosterFail: function (e) {
        console.log(e)
    }
    ,

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    }
    ,

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    }
    ,


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {

    }
    ,

// 保存图片
    bindSaveTap: function (e) {
        wx.saveImageToPhotosAlbum({ //成功之后保存到本地
            filePath: this.data.facePic, //生成的图片的本地路径
            success: function (res) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 2000
                })
            },
            fail: function (res) {

            }
        })
    }
    ,

    url: function (e) {
        ccminiPageHelper.url(e, this);
    }
    ,

// 获取微信用户信息
    getUserProfile: async function (e) {
        wx.getUserProfile({
            desc: '用于完善校庆头像',
            success: async (res) => {
                let userInfo = res.userInfo;
                if (!ccminiHelper.isDefined(userInfo) || !userInfo) {
                    wx.showToast({
                        title: '授权失败，请重新授权',
                        icon: 'none',
                        duration: 3000
                    });
                } else {
                    let url=userInfo.avatarUrl.slice(0,userInfo.avatarUrl.lastIndexOf("/")+1)+'0'
                    this.setData({
                        userPic:url,
                    });
                    await this.catchCreatePosterTap();
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '授权失败，请重新授权',
                    icon: 'none'
                });
            }
        })
    }
    ,
})
